home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / sounds / tools / ulaw2svx.lzh / Ulaw28SVX.c < prev    next >
C/C++ Source or Header  |  1992-01-27  |  6KB  |  258 lines

  1. #include <exec/exec.h>
  2.  
  3. #include <intuition/intuition.h>
  4.  
  5. #include <libraries/asl.h>
  6. #include <libraries/iffparse.h>
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/intuition.h>
  10. #include <proto/asl.h>
  11. #include <proto/dos.h>
  12.  
  13. #include <string.h>
  14.  
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21.  
  22. #include <clib/macros.h>
  23.  
  24. int    ulaw2linear(unsigned char);
  25. void    maketable(signed char *, int);
  26. int    getscale(void);
  27. void    CloseAll(void);
  28.  
  29. /*----------------------------------------------------------------------*/
  30. /*                  IFF 8SVX Stuff                */
  31. /*----------------------------------------------------------------------*/
  32.  
  33. #define BUFSIZE    64000
  34. #define ID_8SVX    MAKE_ID('8','S','V','X')
  35. #define ID_VHDR    MAKE_ID('V','H','D','R')
  36. #define ID_BODY    MAKE_ID('B','O','D','Y')
  37.  
  38. typedef LONG Fixed;    
  39.     /* A fixed-point value, 16 bits to the left of the point and 16 */
  40.     /* to the right. A Fixed is a number of 216ths, i.e. 65536ths.    */
  41.  
  42. #define Unity 0x10000L        /* Unity = Fixed 1.0 = maximum volume    */
  43.  
  44. /* sCompression: Choice of compression algorithm applied to the samples */ 
  45.  
  46. #define sCmpNone       0    /* not compressed            */
  47. #define sCmpFibDelta   1    /* Fibonacci-delta encoding        */ 
  48.  
  49.                 /* Can be more kinds in the future.    */
  50.  
  51. typedef struct {
  52.     ULONG oneShotHiSamples,    /* # samples in the high octave 1-shot part     */
  53.               repeatHiSamples,    /* # samples in the high octave repeat part     */
  54.               samplesPerHiCycle;/* # samples/cycle in high octave, else 0       */
  55.     UWORD samplesPerSec;    /* data sampling rate                */
  56.     UBYTE ctOctave,        /* # octaves of waveforms            */
  57.                 sCompression;    /* data compression technique used        */
  58.     Fixed volume;        /* playback volume from 0 to Unity (full    */
  59.                 /* volume). Map this value into the output    */
  60.                 /* hardware's dynamic range.            */
  61.     } Voice8Header;
  62.  
  63. Voice8Header V8H = { 0, 0, 32, 8363, 1, sCmpNone, Unity };
  64.  
  65. typedef ULONG ID;
  66.  
  67. typedef struct {
  68.     ID    ckID;
  69.     LONG    ckSize;            /* sizeof(ckData) */
  70.     } Chunk;
  71.  
  72. Chunk Form, V8Hdr, Body;
  73.  
  74. /********************************************************************************/
  75.  
  76. BPTR            fpi, fpo;
  77. int            size;
  78. unsigned char        *buf;
  79. struct    IntuitionBase    *IntuitionBase;
  80. struct    Library        *AslBase;
  81. struct    FileRequester    *FR;
  82.  
  83. struct EasyStruct ES1 = {
  84.     sizeof(struct EasyStruct),
  85.     0,
  86.     "Error",
  87.     "Can't %s",
  88.     "EXIT"
  89. };
  90.  
  91. /*--------------------------------------------------------------------------*/
  92. /* The following routine was extracted from posting by Brian Foley.         */
  93. /* Brian Foley        email: bfoley@greatlakes.Central.Sun.COM            */
  94. /* Systems Engineer    smail:    1000 Town Center                            */
  95. /* Sun Microsystems        Suite 1700                                  */
  96. /* GreatLakes Region        Southfield, MI 48075   (313) 352-7070       */
  97. /*--------------------------------------------------------------------------*/
  98.  
  99. int ulaw2linear(unsigned char ulawbyte)
  100. {
  101.     static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
  102.            int sign, exponent, mantissa, sample;
  103.  
  104.     ulawbyte    = ~ulawbyte;
  105.     sign        =  ulawbyte & 0x80;
  106.     exponent    = (ulawbyte >> 4) & 0x07;
  107.     mantissa    =  ulawbyte & 0x0F;
  108.     sample        = exp_lut[exponent] + (mantissa << (exponent + 3));
  109.     if ( sign ) sample = -sample;
  110.     return sample;
  111. }
  112.  
  113. int getscale(void)
  114. {
  115.     int count, max = 0, i;
  116.  
  117.     size    = 0;
  118.  
  119.     do {
  120.         count     = Read(fpi, buf, BUFSIZE);
  121.         size    += count;
  122.         for ( i = 0; i < count; i++ ) {
  123.             max    = MAX(ABS(ulaw2linear(buf[i])), max);
  124.         }
  125.     } while ( count == BUFSIZE );
  126.  
  127.     return max;
  128. }
  129.  
  130. void maketable(signed char *logs, int max)
  131. {
  132.     int i, c, d;
  133.  
  134.     for ( i = 0; i < 256; i++ ) {
  135.         c = ( ulaw2linear(i) * ulaw2linear(0) ) / max;
  136.         d = ABS(c) & 0xFF;
  137.         if ( d > 0x7F ) {
  138.             if ( c > 0 ) {
  139.                 logs[i] = (signed char) ( c / 256 + 1 );
  140.             }
  141.             else {
  142.                 logs[i] = (signed char) ( c / 256 - 1 );
  143.             }
  144.         }
  145.         else {
  146.             logs[i] = (signed char) ( c / 256 );
  147.         }
  148.     }
  149. }
  150.  
  151.  
  152. void CloseAll(void)
  153. {
  154.     if ( FR  )    FreeAslRequest(FR);
  155.     if ( buf )    FreeMem(buf, BUFSIZE);
  156.     if ( fpi )    Close(fpi);
  157.     if ( fpo )    Close(fpo);
  158.  
  159.     if ( IntuitionBase ) {
  160.         DisplayBeep(NULL);
  161.         CloseLibrary((struct Library *) IntuitionBase);
  162.     }
  163.  
  164.     if ( AslBase )    CloseLibrary((struct Library *) AslBase);
  165.     exit(0);
  166. }
  167.  
  168. void main(int argc, char **argv)
  169. {
  170.     int        i, count, max;
  171.     char        ifile[80], ofile[80];
  172.     signed char    logs[256];
  173.     
  174.     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  175.     if ( ! IntuitionBase ) exit(10);
  176.  
  177.     AslBase = OpenLibrary(AslName, 0);
  178.     if ( ! AslBase ) CloseAll();
  179.  
  180.     if ( argc == 0 ) {
  181.         FR = AllocFileRequest();
  182.         if ( ! FR ) {
  183.             CloseAll();
  184.         }
  185.  
  186.         if ( AslRequestTags(FR, ASL_Hail, "Input File", TAG_DONE) ) {
  187.             strmfp(ifile, FR->rf_Dir, FR->rf_File);
  188.         }
  189.         else CloseAll();
  190.  
  191.         if ( AslRequestTags(FR, ASL_Hail, "Output File", TAG_DONE) ) {
  192.             strmfp(ofile, FR->rf_Dir, FR->rf_File);
  193.         }
  194.         else CloseAll();
  195.     }
  196.     else if ( argc != 3 ) {
  197.         FPuts(Output(), "Usage: ");
  198.         FPuts(Output(), argv[0]);
  199.         FPuts(Output(), " infile outfile\n"
  200.                 "Converts from SPARC ULAW to RAW sound format\n");
  201.         exit(0);
  202.     }
  203.     else {
  204.         strcpy(ifile, argv[1]);
  205.         strcpy(ofile, argv[2]);
  206.     }
  207.  
  208.     fpi = Open(ifile, MODE_OLDFILE);
  209.     if ( ! fpi ) {
  210.         EasyRequest(NULL, &ES1, NULL, "open infile");
  211.         CloseAll();
  212.     }
  213.  
  214.     fpo = Open(ofile, MODE_NEWFILE);
  215.     if ( ! fpo ) {
  216.         EasyRequest(NULL, &ES1, NULL, "open outfile");
  217.         CloseAll();
  218.     }
  219.  
  220.     buf = (UBYTE *) AllocMem(BUFSIZE, MEMF_PUBLIC);
  221.     if ( ! buf ) {
  222.         EasyRequest(NULL, &ES1, NULL, "alloc buffer");
  223.         CloseAll();
  224.     }
  225.  
  226.     max = getscale();
  227.     Seek(fpi, 0, OFFSET_BEGINNING);
  228.     maketable(logs, max);
  229.  
  230.     Form.ckID    = ID_FORM;
  231.     Form.ckSize    = 4 + sizeof(Chunk) * 2 + sizeof(Voice8Header) + size;
  232.     Write(fpo, &Form, 8);
  233.  
  234.     i = ID_8SVX;
  235.     Write(fpo, &i, 4);
  236.  
  237.     V8Hdr.ckID        = ID_VHDR;
  238.     V8Hdr.ckSize        = sizeof(Voice8Header);
  239.     Write(fpo, &V8Hdr, 8);
  240.  
  241.     V8H.oneShotHiSamples    = size;
  242.     Write(fpo, &V8H, sizeof(Voice8Header));
  243.  
  244.     Body.ckID        = ID_BODY;
  245.     Body.ckSize        = size;
  246.     Write(fpo, &Body, 8);
  247.  
  248.     do {
  249.         count = Read(fpi, buf, BUFSIZE);
  250.         for ( i = 0; i < count; i++ ) {
  251.             buf[i] = logs[buf[i]];
  252.         }
  253.         if ( count ) Write(fpo, buf, count);
  254.     } while ( count == BUFSIZE );
  255.  
  256.     CloseAll();
  257. }
  258.